package cn.com.thingo.shyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.model.bean.SceneInfoBean;

public class ScenesAdapter extends RecyclerView.Adapter<ScenesAdapter.ViewHolder> {

    private List<SceneInfoBean> sceneList;
    private Context context;
    private OnItemClickListener listener;
    private boolean isSelect = false;
    private boolean isStart = true;
    private boolean canStart = true;

    public boolean isCanStart() {
        return canStart;
    }

    public void setCanStart(boolean canStart) {
        this.canStart = canStart;
    }

    public boolean isStart() {
        return isStart;
    }

    public void setStart(boolean start) {
        isStart = start;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public ScenesAdapter(List<SceneInfoBean> sceneList, Context context) {
        this.sceneList = sceneList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_scenes, parent, false));
    }

    @Override
    public int getItemCount() {
        return sceneList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        SceneInfoBean sceneInfoBean = sceneList.get(position);
        holder.tvName.setText(sceneInfoBean.getSceneInfo().getSceneName());
        holder.ivIcon.setImageResource(R.mipmap.scenes_icon);
        if (isSelect) {
            holder.cbSelector.setVisibility(View.VISIBLE);
            holder.cbSelector.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    sceneList.get(position).setSelectorStauts(isChecked);
                }
            });
            holder.cbSelector.setChecked(sceneInfoBean.isSelectorStauts());
        } else {
            holder.cbSelector.setVisibility(View.GONE);
        }
        if (isStart) {
            holder.ivStatus.setVisibility(sceneInfoBean.isStart() ? View.VISIBLE : View.GONE);
        } else {
            holder.ivStatus.setVisibility(View.GONE);
        }
        if (listener != null) {
            if (canStart) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(position);
                    }
                });
            }
        }
    }

    public String getSelectPositions() {
        StringBuffer result = null;
        for (int i = 0; i < sceneList.size(); i++) {
            if (sceneList.get(i).isSelectorStauts()) {
                if (result == null) {
                    result = new StringBuffer("");
                    result.append(i);
                } else {
                    result.append(",").append(i);
                }
            }
        }
        if (result != null) {
            return result.toString();
        } else {
            return null;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.scenes_icon)
        ImageView ivIcon;
        @BindView(R.id.scenes_status)
        ImageView ivStatus;
        @BindView(R.id.scenes_selector)
        CheckBox cbSelector;
        @BindView(R.id.scenes_name)
        TextView tvName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
